package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.MarketAdExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAdMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminAdServiceImpl implements AdminAdService {
    @Override
    public BaseRespVo list(Integer page, Integer limit, String name, String content, String sort, String order) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketAdMapper mapper = sqlSession.getMapper(MarketAdMapper.class);

        PageHelper.startPage(page, limit);


        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();

        if (!StringUtil.isEmpty(name)) {
            criteria.andNameEqualTo(name);
        }

        if (!StringUtil.isEmpty(content)) {
            criteria.andContentEqualTo(content);
        }

        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            marketAdExample.setOrderByClause(sort + " " + order);
        }
        List<MarketAd> marketAds = mapper.selectByExample(marketAdExample);

        sqlSession.close();
        return BaseRespVo.ok(CommonData.data(new PageInfo(marketAds)));
    }

    @Override
    public BaseRespVo create(String contentStr, String enabledStr, String nameStr, Integer positionInt, String urlStr) {
        Map<Object, Object> createHashMap = new HashMap<>();
        Date date = new Date();
        String addTime = "addTime";
        createHashMap.put(addTime, date);
        String content = "content";
        createHashMap.put(content, contentStr);
        String enabled = "enabled";
        createHashMap.put(enabled, enabledStr);
        String name = "name";
        createHashMap.put(name, nameStr);
        String position = "position";
        createHashMap.put(position, positionInt);
        String updateTime = "updateTime";
        createHashMap.put(updateTime, date);

        //上传图片不能实现
        //todo  上传图片需要实现admin/storage/create
        String url = "url";
        createHashMap.put(url, urlStr);
        return BaseRespVo.ok(createHashMap);
    }

    @SneakyThrows
    @Override
    public BaseRespVo update(JsonNode jsonNode) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketAdMapper mapper = sqlSession.getMapper(MarketAdMapper.class);

        int id = jsonNode.get("id").asInt();
        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();
        criteria.andIdEqualTo(id);


        //通过主键找到要更新的数据
        MarketAd marketAd = new MarketAd();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String addTimeStr = jsonNode.get("addTime").asText();
        Date addTime = simpleDateFormat.parse(addTimeStr);
        String content = jsonNode.get("content").asText();
        boolean deleted = jsonNode.get("deleted").asBoolean();
        boolean enabled = jsonNode.get("enabled").asBoolean();
        String link = jsonNode.get("link").asText();
        String name = jsonNode.get("name").asText();
        int position = jsonNode.get("position").asInt();
        Date now = new Date();
        String url = jsonNode.get("url").asText();

        marketAd.setAddTime(addTime);
        marketAd.setContent(content);
        marketAd.setDeleted(deleted);
        marketAd.setEnabled(enabled);
        marketAd.setId(id);
        marketAd.setLink(link);
        marketAd.setName(name);
        marketAd.setPosition((byte) position);
        marketAd.setUpdateTime(now);
        marketAd.setUrl(url);

        int i = mapper.updateByExample(marketAd, marketAdExample);

        List<MarketAd> marketAds = mapper.selectByExample(marketAdExample);

        sqlSession.commit();
        sqlSession.close();
        return BaseRespVo.ok(marketAds);
    }

    @Override
    public BaseRespVo delete(MarketAd marketAd) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketAdMapper mapper = sqlSession.getMapper(MarketAdMapper.class);
        Integer id = marketAd.getId();

        mapper.deleteByPrimaryKey(id);

        sqlSession.commit();
        sqlSession.close();

        return BaseRespVo.ok();
    }

    @Override
    public void add(MarketAd marketAd) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketAdMapper mapper = sqlSession.getMapper(MarketAdMapper.class);
        mapper.insertSelective(marketAd);
        sqlSession.commit();
        sqlSession.close();
    }
}
